#include "pch.h"
#include "DXAdapter.h"
#include "Helper.h"

using namespace DXBase;
using namespace DirectX;
using namespace Platform;
using namespace Microsoft::WRL;
using namespace Windows::Foundation::Collections;


IIterable<DXAdapter^>^ DXAdapter::GetAdapters()
{
	return ref new EnumAdapter();
}
DXAdapter::DXAdapter(ComPtr<IDXGIAdapter> ada)
	: mAdapter(ada), debug(L"DXAdapter")
{
	mAdapter->GetDesc(&description);
}

EnumAdapter::EnumAdapter()
	: start(0), debug(L"EnumAdapter")
{
	HR2Ex(CreateDXGIFactory1(__uuidof(IDXGIFactory) ,(void**)&mDxgiFactory));
	MoveNext();
}
IIterator<DXAdapter^>^ EnumAdapter::First()
{
	return this;
}
bool EnumAdapter::MoveNext()
{
	current = nullptr;
	ComPtr<IDXGIAdapter> pAdapter; 
	if(mDxgiFactory->EnumAdapters(start, &pAdapter) != DXGI_ERROR_NOT_FOUND) 
		current = ref new DXAdapter(pAdapter);
	start ++;
	return current != nullptr;
}
DXAdapter^ EnumAdapter::Current::get()
{
	return current;
}
bool EnumAdapter::HasCurrent::get()
{
	return current != nullptr;
}
unsigned int EnumAdapter::GetMany(WriteOnlyArray<DXAdapter^>^ items)
{
	unsigned int i = 0,N = items->Length;
	for (; i<N; i++) 
	{
		if (!current)
			break;
		items[i] = current;
		MoveNext();
	}
	return i;
}
